package com.yc.thread.callback;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * 需要返回值的线程测试demo
 */
public class CallBackThread {

    public static void main(String[] args){
        // 创建线程池
        ExecutorService executorService = Executors.newCachedThreadPool();

        // 创建线程容器
        List<Future<String>> result = new ArrayList<>();
        // 执行任务后返回结果，
        for (int i = 0; i < 5; i++) {
            Future<String> submit = executorService.submit(new MyCallable(i));
            result.add(submit);
        }
        // 遍历执行完成后的线程
        result.forEach(e -> {
            if (e.isDone()) {
                try {
                    System.out.println("完成的线程 " + e.get());
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                } catch (ExecutionException executionException) {
                    executionException.printStackTrace();
                }
            } else {
                try {
                    System.out.println("未完成的线程 " + e.get());
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                } catch (ExecutionException executionException) {
                    executionException.printStackTrace();
                }
            }
        });

        executorService.shutdown();
    }
}
